//
//  StatusRetweetedView.swift
//  sinaOnceAgain
//
//  Created by 王相超 on 16/2/23.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit
import SnapKit

class StatusRetweetedView: UIView {
    
    //添加约束对象属性  做记录使用
    var bottomConstaint: Constraint?
    
    var retweetStatus: Status? {
        
        didSet{
            //设置数据
            reweetedLabel.text = retweetStatus?.text
            pictureView.imageURLS = retweetStatus?.imageURLs
            
            //卸载老的约束对象
            self.bottomConstaint?.uninstall()
            
            //根据数据调整约束关系
            if let urls = retweetStatus?.imageURLs where urls.count != 0 {
                
                //转发微博有配图
                self.snp_makeConstraints { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                }
            }else{
                //转发微博没有配图
                self.snp_makeConstraints { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(reweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                }
            }
        }
        
    }
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //设置界面
    private func setupUI() {
        
        addSubview(reweetedLabel)
        addSubview(pictureView)
        
        //设置约束
        reweetedLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(reweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(reweetedLabel.snp_left)
        }
        
        //转发微博的底部需要添加约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
    }
    
    //懒加载文字
    private lazy var reweetedLabel: UILabel = UILabel(text: "转发微博: 摸摸大", fontSize: 14, textColor: UIColor.darkGrayColor() , alignment: .Left)
    
    //配图
    private lazy var pictureView: StatusPictureView = StatusPictureView()
    
}
